﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EMPower.Enum;
using BLL.Common;
using Module;
using BLL;
using Enum;
public partial class Login : System.Web.UI.Page
{
    protected string ipAddress = string.Empty;
    protected string loginUserName = string.Empty;
    protected string loginPassword = string.Empty;
    protected string validationCode = string.Empty;
    protected string validateCodeRefresh = string.Empty;
    protected string alertMessageUserName = string.Empty;
    protected string alertMessagePassword = string.Empty;
    protected string alertMessageValidateCode = string.Empty;

    private string goUrl = string.Empty;
    //protected string l = Contexts.GetEncrypt(Convert.ToInt32(LanguageCode.Chinese_PRC).ToString());

    private const string COOKIES_NAME = "EMPOWER_USERINFO_ADMIN"; //用户cookie名称
    private const char COOKIES_SPLITER = '-';       //cookie中的分隔符，用户名-密码。例如：张三-123
    private const string COOKIES_Language = "EMPOWER_LANGUAGE_ADMIN";


    protected void Page_Load(object sender, EventArgs e)
    {
        ipAddress = Request.UserHostAddress;
        goUrl = Community.Utility.Contexts.GetStringFromRequest(HttpContext.Current, "goUrl");

        if (!IsPostBack)
        {
            btnSubmit.Attributes.Add("onclick", "return checkForm();");

            string userinfo = string.Empty;
            //根据cookie取得登录名
            if (Request.Cookies[COOKIES_NAME] != null)
            {
                userinfo = Request.Cookies[COOKIES_NAME].Value;
                if (!string.IsNullOrEmpty(userinfo))
                {
                    userinfo = Contexts.GetDecrypt(userinfo);
                    if (!string.IsNullOrEmpty(userinfo))
                    {
                        if (userinfo.IndexOf(COOKIES_SPLITER) > 0)
                        {
                            string username = userinfo.Split(new char[] { COOKIES_SPLITER })[0];
                            //password = userinfo.Split(new char[] { COOKIES_SPLITER })[1];

                            this.txtLoginName.Text = username;
                            //this.Txt_Password.Value = password;
                            //this.Txt_Password.Attributes.Add();
                            //rememberPassword.Checked = true;
                        }
                    }
                }
            }

        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string userName = this.txtLoginName.Text.Trim();
        string password = this.txtPassword.Text.Trim();
        string validateCode = this.txtValidateCode.Text.Trim();

        if (string.IsNullOrEmpty(userName))
        {
            div_errorInfo.Visible = true;
            errorMessage.Text ="用户名不能为空";
        }
        else if (string.IsNullOrEmpty(password))
        {
            div_errorInfo.Visible = true;
            errorMessage.Text = "密码不能为空";
        }
        else if (string.IsNullOrEmpty(validateCode))
        {
            div_errorInfo.Visible = true;
            errorMessage.Text = "验证码不能为空";// lh.GetLabel("UserLogin.NullOfValidateCode");
        }
        else
        {
            if (Session["ValidateCode"] == null)
            {
                div_errorInfo.Visible = true;
                errorMessage.Text = "请刷新验证码";//  lh.GetLabel("UserLogin.TimeoutOfValidateCode");
            }
            else
            {
                if (Session["ValidateCode"].ToString().ToLower() != validateCode.ToLower())
                {
                    //errorMessage.Text = string.Format(lh.GetLabel("UserLogin.WrongValidateCode"), Session["ValidateCode"].ToString());
                    div_errorInfo.Visible = true;
                    errorMessage.Text ="验证码错误";
                    Session["ValidateCode"] = null;
                }               
                else
                {

                    //UserPassportInfo userPassportInfo = BLL.IDAL.BUserPassport.getInstance().GetUserPassportInfo(userName, password);
                    UserPassportInfo userPassportInfo = BUserPassport.getInstance().GetUserPassportInfo(userName);
                    if (userPassportInfo == null || userPassportInfo.UserPassportId == 0)
                    {
                        div_errorInfo.Visible = true;
                        errorMessage.Text = "用户名不存在";//  lh.GetLabel("UserLogin.WrongUserNameOrPassword");

                    }
                    else if (!password.Equals(userPassportInfo.Password))
                    {
                        div_errorInfo.Visible = true;
                        errorMessage.Text = "密码错误";//  lh.GetLabel("UserLogin.WrongUserNameOrPassword");
                    }
                    else
                    {
                        if (userPassportInfo.Status != 1)
                        {
                            div_errorInfo.Visible = true;
                            errorMessage.Text = "用户状态不合法";
                        }
                        UserProfileInfo profile = BUserProfile.getInstance().GetUserProfileInfo(userPassportInfo.UserPassportId);
                        if (profile.UserType != Convert.ToInt32(UserType.Admin))
                        {
                            div_errorInfo.Visible = true;
                            errorMessage.Text = "没有权限登录后台";
                        }
                        else
                        {
                            userPassportInfo.LoginCount += 1;
                            userPassportInfo.LastLogin = DateTime.Now;
                            BUserPassport.getInstance().Update(userPassportInfo);
                            userPassportInfo.ProfileInfo = profile;
                            ///插入Session
                            ///
                            HttpContext.Current.Session["UserLoginInfo"] = userPassportInfo;

                            //写入cookie
                            //string rememberPwd = BLL.Global.Contexts.GetStringFromRequest("rememberPassword");
                            if (this.Checkbox1.Checked)
                            {
                                //储存用户名，密码信息
                                string cookieInfo = this.txtLoginName.Text.Trim() + COOKIES_SPLITER.ToString() + this.txtPassword.Text.Trim();

                                HttpCookie cookie = new HttpCookie(COOKIES_NAME, Contexts.GetEncrypt(cookieInfo));
                                cookie.Expires = DateTime.Now.AddDays(30);//30天后自动过期
                                Response.Cookies.Add(cookie);//添加到客户端session则存在服务器。
                            }
                            else
                            {
                                HttpCookie cookie = Request.Cookies[COOKIES_NAME];
                                if (null != cookie)
                                {
                                    cookie.Expires = DateTime.Now.AddMilliseconds(-1);
                                    Response.Cookies.Add(cookie);
                                }
                                //Request.Cookies.Remove(COOKIES_NAME);
                            }

                            string redirectUrl = string.Empty;  //重定向页面地址 
                            if (goUrl != string.Empty)
                            {
                                redirectUrl = goUrl;
                            }
                            else
                            {
                                redirectUrl = Contexts.AdminPath + "/Index.aspx";
                            }

                            Response.Redirect(redirectUrl);
                        }
                    }
                }
            }
        }
    }

    protected void reset_Click(object sender, EventArgs e)
    {
        this.txtLoginName.Text = string.Empty;
        this.txtPassword.Text = string.Empty;
        this.errorMessage.Text = string.Empty;
        div_errorInfo.Visible = false;

    }
}